package net.zjitc.androidclassviewdraw;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(2);
        //点
        canvas.drawPoint(500, 500, paint);
        //圆形
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(500, 500, 200, paint);
        //椭圆
        paint.setStyle(Paint.Style.STROKE);
        RectF oval = new RectF(200, 200, 800, 800);
        canvas.drawOval(oval, paint);
        //矩形
        Rect rect = new Rect(300, 300, 700, 700);
        canvas.drawRect(rect, paint);
        //圆角矩形
        RectF rectRound = new RectF(300, 300, 700, 700);
        canvas.drawRoundRect(rectRound, 75, 75, paint);
        //线
        canvas.drawLine(300, 500, 500, 500, paint);
        //bitmap图片
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.timg);
        float viewWidth=getWidth();
        float bitmapHeight=bitmap.getHeight();
        float bitmaoWidth=bitmap.getWidth();
        float showHeight=(viewWidth/bitmaoWidth)*bitmapHeight;
        canvas.drawBitmap(bitmap,null,new RectF(0,900,viewWidth,showHeight+900),paint);
    }
}
